//
//  FeedbackInputView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/6/11.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "FeedbackInputView.h"

@interface FeedbackInputView ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UITextView *placeHolderView;
@property (nonatomic, strong) UILabel *maxLimitLab;

@end

@implementation FeedbackInputView

- (NSString *)text
{
    return self.textView.text;
}

- (void)setupSubViews
{
    self.textView = [[UITextView alloc] init];
    self.textView.textColor = kColor333333;
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.textContainerInset = UIEdgeInsetsMake(-2, -5, -2, -5);
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.delegate = self;
    [self addSubview:self.textView];
    
    self.placeHolderView = [[UITextView alloc] init];
    self.placeHolderView.textColor = kColor999999;
    self.placeHolderView.font = [UIFont systemFontOfSize:14];
    self.placeHolderView.text = @"请输入您的意见反馈";
    self.placeHolderView.editable = false;
    self.placeHolderView.selectable = false;
    self.placeHolderView.textContainerInset = self.textView.textContainerInset;
    self.placeHolderView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.placeHolderView];
    
    self.maxLimitLab = [[UILabel alloc] init];
    self.maxLimitLab.text = @"0/300";
    self.maxLimitLab.font = [UIFont systemFontOfSize:14];
    self.maxLimitLab.textColor = kColor999999;
    [self addSubview:self.maxLimitLab];
    
    [self bringSubviewToFront:self.textView];
}

- (void)setupConstraints
{
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.top.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-16);
        make.bottom.equalTo(self).offset(-15);
    }];
    
    [self.placeHolderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.textView);
    }];
    
    [self.maxLimitLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-12);
        make.bottom.equalTo(self).offset(-8);
    }];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeHolderView.alpha = 0;
    }
    else {
        self.placeHolderView.alpha = 1;
    }
    self.maxLimitLab.text = [NSString stringWithFormat:@"%lu/300",(unsigned long)textView.text.length];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView.text.length + text.length > 100) {
        if ([text isEqualToString:@""]) {
            return YES;
        }
        NSMutableString *replacementText = text.mutableCopy;
        NSString *temp = [replacementText substringToIndex:30-textView.text.length];
        NSMutableString *result = textView.text.mutableCopy;
        [result appendString:temp];
        textView.text = result.copy;
        return NO;
    }
    return YES;
}


@end
